/* Copyright 2008. All rights reserved. */
package org.bling.parser.tags;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import org.bling.parser.BlingParserException;
import org.bling.parser.expressions.ExpressionManager;
import org.bling.process.context.ApplicationContext;
import org.bling.swt.widgets.ImageCanvas;
import org.bling.util.Constants;
import org.bling.util.Util;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Widget;

/**
 * renders a graphic/image inside of an SWT Canvas
 * 
 * @author DISBROB
 * 
 */
public class ImageTag extends Tag {

	protected ImageCanvas image;
	protected String src;

	public void setSrc(String src) {
		this.src = src;
	}

	@Override
	public int doStartTag() throws BlingParserException {

		String evalSrc = ExpressionManager.evaluate(src, context).toString();
		
		if (Util.isBlankOrNull(evalSrc)) {
			throw new BlingParserException("SRC Required");
		}

		if (!(parent.getWidget() instanceof Composite)) {
			throw new BlingParserException(
					"Label tags can only be inside of composites");
		}
		
		
		
		Composite composite = (Composite) parent.getWidget();

		image = new ImageCanvas(composite, getImage(evalSrc));
//		canvas = new Canvas(composite, SWT.NO_REDRAW_RESIZE);
//		canvas.addPaintListener(new PaintListener() {
//	        public void paintControl(PaintEvent e) {
//	        	System.out.println("paintControl(" + e + ")");
//	        	Display display = (Display) ApplicationContext.getInstance()
//				.getAttribute(Constants.ApplicationContextAttributes.DISPLAY);
//	        	Image image = new Image(display, src);
//	        	
//	        	canvas.setSize(image.getImageData().width, image.getImageData().height);
//	            e.gc.drawImage(image,0,0);
//	            
//	        }
//	    });
		
		return EVAL_PAGE;
	}

	@Override
	public Widget getWidget() throws BlingParserException {
		return image;
	}

	private Image getImage(String src) {
    	Display display = (Display) ApplicationContext.getInstance()
		.getAttribute(Constants.ApplicationContextAttributes.DISPLAY);
    	
		
		try {
			URI uri = new URI(src);

			if (uri.getScheme() == null) {
				return new Image(display, src);
			}

			URL url = new URL(src);
			InputStream is = url.openStream();
			return new Image(Display.getCurrent(), is);
		} catch (FileNotFoundException e) {
			//e.printStackTrace();
			System.out.println("WARNING - FileNotFoundException IN ImageTag");
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			System.out.println("WARNING - URISyntaxException IN ImageTag");
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			System.out.println("WARNING - MalformedURLException IN ImageTag");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			System.out.println("WARNING - IOException IN ImageTag");
		}
		
		//return new Image(display, 0, 0);
		return null;
	}
}
